class User:
    def __init__(self):
        self.user_info = {"root": {"pwd": "123456",
                                   "money": 1000}}
        self.good_info = {"apple": {"unit_price": "8", "inventory": 30},
                          "orange": {"unit_price": "4", "inventory": 100}}

    def register(self):
        print("<注册模式>".center(50, "*"))
        global name, money
        name = input("请输入用户名：")
        if name in self.user_info.keys():
            print("该用户已被注册，请重新输入新用户名！")
            self.register()
        else:
            pwd = input("请输入密码：")
            money = int(input("请输入要充值金额："))
            if money >= 0:
                self.user_info[name] = {"pwd": pwd, "money": money}
                print(self.user_info)
                print("注册成功！")
            else:
                print("请输入正确的金额！")

    def login(self):
        print("<登录模式>".center(50, "*"))
        global user_name
        for i in range(3):
            user_name = input("请输入用户名：")
            if user_name in self.user_info.keys():
                pwd = input("请输入密码：")
                if pwd == self.user_info[user_name]["pwd"]:
                    print("登录成功！")
                    break
                else:
                    print("密码错误！请重新输入密码！")
            else:
                print("用户名错误，请重新输入！")
            print(f"这是第{i+1}次输入错误，还剩{2-i}次机会！")
        else:
            print("超过最大验证次数，登录失败！")


    def buy_goods(self):
        print("<购物模式>".center(50, "*"))
        goods = input("请输入要购买的商品：")
        if goods in self.good_info.keys():
            weight = int(input("请输入要购买的重量："))
            if weight > int(self.good_info[goods]["inventory"]):
                print("库存不足！")
            else:
                pay = int(weight * int(self.good_info[goods]["unit_price"]))
                if int(self.user_info[user_name]["money"]) >= pay:
                    self.user_info[user_name]["money"] -= pay
                    self.good_info[goods]["inventory"] -= weight
                    print(f"购买成功！{user_name}剩余{self.user_info[user_name]['money']}元，"
                          f"{goods}剩余库存{self.good_info[goods]['inventory']}斤")
                else:
                    print("余额不足！")


class Goods:
    def __init__(self):
        self.good_info = {"apple": {"unit_price": "8", "inventory": 30},
                          "orange": {"unit_price": "4", "inventory": 100}}

    def show_goods(self):
        print("商品仓".center(50, "*"))
        print("类别  名称  单价  库存".center(48, "*"))
        print("水果  apple  8元/斤  30斤".center(50, "*"))
        print("水果  orange  4元/斤  100斤".center(52, "*"))


def main():
    user = User()
    goods = Goods()
    while True:
        print("欢迎使用三创购物系统！".center(100, "*"))
        print("注册请输入1，登录请输入2，购物请按3，退出请输入0".center(96, "*"))
        select = input("请选择您需要进行的操作：")
        if select == "1":
            user.register()
        elif select == "2":
            user.login()
        elif select == "3":
            goods.show_goods()
            user.buy_goods()
        elif select == "0":
            print("退出系统！欢迎再次使用！")
        else:
            print("输入有误，请重新输入！")


main()
